package com.herui.iot.data.check.source;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.herui.sdyu_lib.http.callback.Callback;
import com.herui.sdyu_lib.utils.JsonUtils;
import com.herui.sport_care.data.BaseCallBack;
import com.herui.sport_care.data.check.AgreementResponse;
import com.herui.sport_care.data.check.BetaDataFileUploadRequest;
import com.herui.sport_care.data.check.BetaDataUploadForm;
import com.herui.sport_care.data.check.DeviceInfo;
import com.herui.sport_care.data.check.EquipmentType;
import com.herui.sport_care.data.check.EvalUserInfo;
import com.herui.sport_care.data.check.SecurityToken;
import com.herui.sport_care.request.CheckRequest;
import com.herui.sport_care.request.SportResponse;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.herui.sport_care.config.ServerConfig.retrofitCall;

/**
 * Created by amanda.shan on 2021/9/30.
 */
public class CheckRepository implements CheckSource {

    private static CheckRepository instance;

    public static CheckRepository getInstance() {
        if (instance == null) {
            synchronized (CheckRepository.class) {
                if (instance == null) {
                    instance = new CheckRepository();
                }
            }
        }
        return instance;
    }

    public void clearAll() {

    }

    @Override
    public void getOssToken(Callback.CommonCallback<SecurityToken> callback) {
        retrofitCall.call(retrofitCall.conver(CheckRequest.class).getOssToken(),
                new BaseCallBack<String>(new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        JSONObject jsonObject = JSON.parseObject(result);
                        SecurityToken credentials =
                                JSON.parseObject(jsonObject.get("credentials").toString(),
                                        SecurityToken.class);
                        callback.onSuccess(credentials);
                    }

                    @Override
                    public void onError(Throwable e, String code, String message) {
                        callback.onError(e, code, message);
                    }
                }) {
                    @Override
                    public SportResponse<String> getApiResponse(String s) throws Exception {
                        return JsonUtils.parseObject(s,
                                new TypeReference<SportResponse<String>>() {
                                });
                    }

                });

    }

    @Override
    public void findAwaitItemList(Long evaluationSchemeId, int type, long appraiserId,
                                  Callback.CommonCallback<List<EvalUserInfo>> callback) {
        Map<String, Object> map = new HashMap<>();
        map.put("evaluationSchemeId", evaluationSchemeId);
        map.put("type", type);
        map.put("appraiserId", appraiserId);
        retrofitCall.call(retrofitCall.conver(CheckRequest.class).findAwaitItemList(map),
                new BaseCallBack<List<EvalUserInfo>>(callback) {
                    @Override
                    public SportResponse<List<EvalUserInfo>> getApiResponse(String s) throws Exception {
                        return JsonUtils.parseObject(s,
                                new TypeReference<SportResponse<List<EvalUserInfo>>>() {
                                });
                    }

                });
    }

    @Override
    public void getAvailableEquipmentList(Long evaluationSchemeId, long organId, long userId,
                                          EquipmentType equipmentType,
                                          Callback.CommonCallback<List<DeviceInfo>> callback) {
        Map<String, Object> map = new HashMap<>();
        map.put("evaluationSchemeId", evaluationSchemeId);
        map.put("organId", organId);
        map.put("userId", userId);
        map.put("equipmentType", equipmentType);
        retrofitCall.call(retrofitCall.conver(CheckRequest.class).getAvailableEquipmentList(map),
                new BaseCallBack<List<DeviceInfo>>(callback) {
                    @Override
                    public SportResponse<List<DeviceInfo>> getApiResponse(String s) throws Exception {
                        return JsonUtils.parseObject(s,
                                new TypeReference<SportResponse<List<DeviceInfo>>>() {
                                });
                    }

                });
    }

    @Override
    public void equipmentUserSchemeBind(Long evaluationSchemeId, long equipmentId, long userId,
                                        EquipmentType equipmentType,
                                        Callback.CommonCallback<String> callback) {
        Map<String, Object> map = new HashMap<>();
        map.put("evaluationSchemeId", evaluationSchemeId);
        map.put("equipmentId", equipmentId);
        map.put("userId", userId);
        map.put("equipmentType", equipmentType);
        retrofitCall.call(retrofitCall.conver(CheckRequest.class).equipmentUserSchemeBind(map),
                new BaseCallBack<String>(callback) {
                    @Override
                    public SportResponse<String> getApiResponse(String s) throws Exception {
                        return JsonUtils.parseObject(s,
                                new TypeReference<SportResponse<String>>() {
                                });
                    }

                });
    }

    @Override
    public void equipmentUserSchemeUnBind(Long evaluationSchemeId, long equipmentId, long userId,
                                          Callback.CommonCallback<String> callback) {
        Map<String, Object> map = new HashMap<>();
        map.put("evaluationSchemeId", evaluationSchemeId);
        map.put("equipmentId", equipmentId);
        map.put("userId", userId);
        retrofitCall.call(retrofitCall.conver(CheckRequest.class).equipmentUserSchemeUnBind(map),
                new BaseCallBack<String>(callback) {
                    @Override
                    public SportResponse<String> getApiResponse(String s) throws Exception {
                        return JsonUtils.parseObject(s,
                                new TypeReference<SportResponse<String>>() {
                                });
                    }

                });
    }

    @Override
    public void dataUpload(BetaDataUploadForm betaDataUploadForm,
                           Callback.CommonCallback<String> callback) {
        retrofitCall.call(retrofitCall.conver(CheckRequest.class).dataUpload(betaDataUploadForm),
                new BaseCallBack<String>(callback) {
                    @Override
                    public SportResponse<String> getApiResponse(String s) throws Exception {
                        return JsonUtils.parseObject(s,
                                new TypeReference<SportResponse<String>>() {
                                });
                    }

                });
    }

    @Override
    public void getAgreement(Long agreementId, Callback.CommonCallback<AgreementResponse> callback) {
        retrofitCall.call(retrofitCall.conver(CheckRequest.class).getAgreement(agreementId),
                new BaseCallBack<AgreementResponse>(callback) {
                    @Override
                    public SportResponse<AgreementResponse> getApiResponse(String s) throws Exception {
                        return JsonUtils.parseObject(s,
                                new TypeReference<SportResponse<AgreementResponse>>() {
                                });
                    }

                });
    }

    @Override
    public void fileDataUpload(BetaDataFileUploadRequest betaDataFileUploadRequest,
                               Callback.CommonCallback<String> callback) {
        retrofitCall.call(retrofitCall.conver(CheckRequest.class).fileDataUpload(betaDataFileUploadRequest),
                new BaseCallBack<String>(callback) {
                    @Override
                    public SportResponse<String> getApiResponse(String s) throws Exception {
                        return JsonUtils.parseObject(s,
                                new TypeReference<SportResponse<String>>() {
                                });
                    }

                });
    }

    @Override
    public void uploadRpe(Long betaDataId, String rpe, Callback.CommonCallback<String> callback) {
        Map<String, Object> map = new HashMap<>();
        map.put("betaDataId", betaDataId);
        map.put("rpe", rpe);
        retrofitCall.call(retrofitCall.conver(CheckRequest.class).uploadRpe(map),
                new BaseCallBack<String>(callback) {
                    @Override
                    public SportResponse<String> getApiResponse(String s) throws Exception {
                        return JsonUtils.parseObject(s,
                                new TypeReference<SportResponse<String>>() {
                                });
                    }

                });
    }
}
